<?php

namespace App\Admin\Controllers;

use App\Models\ShortUrl;
use App\Admin\Repositories\ViewCount;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class ViewCountController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new ViewCount(), function (Grid $grid) {
            $grid->model()->orderBy('created_at','desc');
            // $grid->column('id')->sortable()->filter();
            $grid->column('category')->using([1 => '入口', 2 => '落地页',3=>'播放页',4=>'支付页',5=>'支付成功'])->label([
                'default' => 'primary', // 设置默认颜色，不设置则默认为 default
            
                1 => 'primary',
                2 => 'danger',
                3 => 'warning',
                4 => 'info',
                5 => 'success',
            ])->sortable()->filter()->help("1:入口, 2:落地页,3:播放页,4:支付页,5:支付成功");
            $grid->column('短链')->display(function(){
                $short = ShortUrl::where('key',$this->code)->value('link');
                return $short??'非入口';
            });
            $grid->column('agent_id')->filter();
            $grid->column('openid')->filter();
            $grid->column('ip')->sortable()->filter();
            $grid->column('tips')->display(function(){
                return "<span style='color:red;'>$this->tips</span>";
            })->sortable();
            // $grid->column('path')->sortable()->filter();
            $grid->column('url')->sortable()->limit(50);
            $grid->column('ua')->sortable()->limit(60)->filter();
            // $grid->column('code')->limit(20);
            $grid->column('created_at')->sortable();
        
            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('ip');
        
            });
            $grid->disableCreateButton();
            $grid->actions(function (Grid\Displayers\Actions $actions) {
                $actions->disableView();
                $actions->disableEdit();
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new ViewCount(), function (Show $show) {
            $show->field('id');
            $show->field('ip');
            $show->field('category');
            $show->field('path');
            $show->field('url');
            $show->field('ua');
            $show->field('agent_id');
            $show->field('code');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new ViewCount(), function (Form $form) {
            $form->display('id');
            $form->text('ip');
            $form->text('category');
            $form->text('path');
            $form->text('url');
            $form->text('ua');
            $form->text('agent_id');
            $form->text('code');
        
            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
